<template>
  <div class="container">
    <h2 class="title">{{$t('m.container.info')}}</h2>
    <el-form ref="refForm" :model="formData" :rules="rules">
      <el-form-item :label="$t('m.container.name')" prop="strategyName" label-width="100px">
        <el-input v-model="formData.strategyName"></el-input>
      </el-form-item>
      <el-table :data="formData.groupList" style="width: 100%">
        <el-table-column align="center" type="index" width="100"></el-table-column>
        <el-table-column align="center" :label="$t('m.page.region')">
          <template slot-scope="scope">
            <el-form-item :prop="'groupList.'+scope.$index+'.areaids'" :rules="rules.area">
              <el-select @visible-change="filterArea(scope.row,$event)" v-model="scope.row.areaids" multiple filterable placeholder>
                <el-option-group v-for="(group,index) in partitionList" :key="group.partitionname+index" :label="group.partitionname">
                  <el-option :disabled="item.isdisabled" style="padding-left:50px" v-for="(item,i) in group.warehouseareas" :key="item.areaname+i" :label="item.areaname" :value="item.areaid+','+item.areaname"></el-option>
                </el-option-group>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('m.operation')" width="100">
          <template slot-scope="scope">
            <el-button type="danger" @click="deleteRow(scope.$index)" size="mini">{{$t('m.delete')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div align="left">
        <el-button type="primary" size="mini" @click="addRow">{{$t('m.add')}}</el-button>
      </div>
      <div class="btns">
        <el-button type="primary" @click="save">{{$t('m.save')}}</el-button>
        <el-button @click="back">{{$t('m.cancel')}}</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name:'',
  data(){
    return {
      formData:{
        strategyName:'',
        groupList:[]
      },
      partitionList:[]
    }
  },
  props:['obj'],
  computed:{
    rules(){
      return {
        strategyName:[
          {
            required:true,
            message:this.$t('m.enter') + this.$t('m.container.name'),
            trigger:'blur'
          }
        ],
        area:[
          {
            required:true,
            message:this.$t('m.choose') + this.$t('m.page.region'),
            trigger:'blur'
          }
        ]
      }
    }
  },
  watch:{
  },
  methods:{
    getInfo(strategyId){
      this.$axios({
        method:'get',
        url:`/schedulingStrategy/group/${strategyId}`
      }).then(({ data }) => {
        this.formData = data
        this.formData.groupList.forEach(element => {
          this.$set(element, 'areaids', element.areas.map(v => {
            return v.areaid + ',' + v.areaname
          }))
        })
      })
    },
    getPartition(){
      return new Promise((resolve) => {
        this.$axios({
          method:'get',
          url:`/areascheduing/partition/list`
        }).then(({ data }) => {
          this.partitionList = data.filter((v) => {
            return v.warehouseareas.length > 0
          })
          this.partitionList.forEach(element => {
            element.warehouseareas.forEach(e => {
              this.$set(e, 'isdisabled', false)
            })
          })
          let partitionid = this.partitionList[0].partitionid
          let areaid = this.partitionList[0].warehouseareas[0].areaid
          resolve({ partitionid, areaid })
        })
      })
    },
    filterArea(row, event){
      if (event === true){
        this.partitionList.forEach(element => {
          element.warehouseareas.forEach(e => {
            this.$set(e, 'isdisabled', false)
          })
        })
        this.partitionList.forEach(element => {
          element.warehouseareas.forEach(e => {
            this.formData.groupList.forEach(item => {
              item.areaids.forEach(i => {
                let arr = i.split(',')
                if (e.areaid == arr[0]){
                  this.$set(e, 'isdisabled', true)
                }
              })
            })
          })
        })
        this.partitionList.forEach(element => {
          element.warehouseareas.forEach(e => {
            row.areaids.forEach(i => {
              let arr = i.split(',')
              if (e.areaid == arr[0]){
                this.$set(e, 'isdisabled', false)
              }
            })
          })
        })
      }
    },
    deleteRow(index){
      this.formData.groupList.splice(index, 1)
    },
    save(){
      this.formData.groupList.forEach(element => {
        element.areas = []
        if(element.areaids){//不加判断，当点击添加不输入内容后返回在添加点保存就会报错
          element.areaids.forEach(e => {
            let arr = e.split(',')
            element.areas.push({
              areaid:arr[0],
              areaname:arr[1]
            })
          })
        }
      })
      this.$refs['refForm'].validate(valid => {
        if (valid){
          this.$axios({
            method:'post',
            url:`/schedulingStrategy/save`,
            data:this.formData
          }).then(() => {
            this.$message.success(this.$t('m.quality.success'))
            this.back()
          })
        }
      })
    },
    back(){
      let obj = {
        componentName:'scheduling'
      }
      this.$emit('switchComponent', obj)
    },
    addRow(){
      this.formData.groupList.push({
        areas:[],
        groupId:null
      })
    }
  },
  async activated(){
    this.$nextTick(()=>{
      this.$refs.refForm.resetFields()//等弹窗里的form表单的dom渲染完在执行this.$refs.staffForm.resetFields()，去除验证
    })
    await this.getPartition()
    //this.$refs.refForm.clearValidate()
    if (this.obj.strategyId){
      this.getInfo(this.obj.strategyId)
    } else {
      this.formData = {
        groupList:[
          {
            areas:[
              {
                areaid:null,
                areaname:''
              }
            ]
          }
        ],
        strategyId:null,
        strategyName:''
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
}
.title {
  margin-bottom: 30px;
}
::v-deep.el-table .el-form-item{
  margin-bottom: 0;
}
</style>
